package com.hzxy.yiyid.web.tool;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @ClassName Base64Tool
 * @Description TOOD
 * @Author RAICOM2023
 * @Date 2023/5/1 17:10
 * @Version 1.0
 **/
public class Base64Tool {

    /**
     * BASE64加密
     * @param bytes
     * @return
     */
    public static String encode(byte[] bytes) {
        byte[] bt = bytes;
        return Base64.getEncoder().encodeToString(bt).replaceAll("\r|\n", "");
    }

    /**
     * BASE64加密
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode(String str) throws UnsupportedEncodingException {
        byte[] bt = str.getBytes("UTF-8");
        return encode(bt);
    }

    /**
     * BASE64解密
     * @param str
     * @return
     * @throws IOException
     */
    public static byte[] decode2Byte(String str) {
        return Base64.getDecoder().decode(str);
    }

    /**
     * BASE64解密
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decode(String str) throws UnsupportedEncodingException {
        return new String(decode2Byte(str), "UTF-8");
    }

}
